package com.ruoyi.slope.video.radar.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.slope.device.deviceInfo.domain.SlopeDeviceInfo;
import com.ruoyi.slope.device.deviceInfo.mapper.SlopeDeviceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.slope.video.radar.mapper.SlopeRadarMonitorMapper;
import com.ruoyi.slope.video.radar.domain.SlopeRadarMonitor;
import com.ruoyi.slope.video.radar.service.ISlopeRadarMonitorService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 边坡雷达Service业务层处理
 *
 * @author psy
 * @date 2025-05-28
 */
@Service
public class SlopeRadarMonitorServiceImpl implements ISlopeRadarMonitorService {
    @Autowired
    private SlopeRadarMonitorMapper slopeRadarMonitorMapper;
    @Autowired
    private SlopeDeviceInfoMapper slopeDeviceInfoMapper;

    /**
     *
     * @param file 文件
     * @param deviceId 设备ID
     * @param apiKey 权限Key
     * @throws Exception 抛出异常，防止程序崩溃
     */
    @Override
    public void handleFile(MultipartFile file, String deviceId, String apiKey) throws Exception {
        // 1. 上传文件，返回的是相对路径（如 /profile/upload/2025/05/28/xxx.json）
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);

        SlopeDeviceInfo deviceInfo = new SlopeDeviceInfo();
        deviceInfo.setDeptId(100L);
        deviceInfo.setDeviceSn(deviceId);
        List<SlopeDeviceInfo> device = slopeDeviceInfoMapper.selectSlopeDeviceInfoList(deviceInfo);
        deviceInfo = device.get(0);

        SlopeRadarMonitor slopeRadarMonitor = new SlopeRadarMonitor();
        slopeRadarMonitor.setDeformationFile(filePath);
        slopeRadarMonitor.setSlopeCode(deviceInfo.getMonitorId());
        slopeRadarMonitor.setDeptId(100L);
        slopeRadarMonitor.setSensorId(deviceId);

        slopeRadarMonitorMapper.insertSlopeRadarMonitor(slopeRadarMonitor);
    }

    /**
     * 查询边坡雷达
     *
     * @param id 边坡雷达主键
     * @return 边坡雷达
     */
    @Override
    public SlopeRadarMonitor selectSlopeRadarMonitorById(Long id) {
        return slopeRadarMonitorMapper.selectSlopeRadarMonitorById(id);
    }

    /**
     * 查询边坡雷达列表
     *
     * @param slopeRadarMonitor 边坡雷达
     * @return 边坡雷达
     */
    @Override
    public List<SlopeRadarMonitor> selectSlopeRadarMonitorList(SlopeRadarMonitor slopeRadarMonitor) {
        return slopeRadarMonitorMapper.selectSlopeRadarMonitorList(slopeRadarMonitor);
    }

    /**
     * 新增边坡雷达
     *
     * @param slopeRadarMonitor 边坡雷达
     * @return 结果
     */
    @Override
    public int insertSlopeRadarMonitor(SlopeRadarMonitor slopeRadarMonitor) {
        return slopeRadarMonitorMapper.insertSlopeRadarMonitor(slopeRadarMonitor);
    }

    /**
     * 修改边坡雷达
     *
     * @param slopeRadarMonitor 边坡雷达
     * @return 结果
     */
    @Override
    public int updateSlopeRadarMonitor(SlopeRadarMonitor slopeRadarMonitor) {
        return slopeRadarMonitorMapper.updateSlopeRadarMonitor(slopeRadarMonitor);
    }

    /**
     * 批量删除边坡雷达
     *
     * @param ids 需要删除的边坡雷达主键
     * @return 结果
     */
    @Override
    public int deleteSlopeRadarMonitorByIds(Long[] ids) {
        return slopeRadarMonitorMapper.deleteSlopeRadarMonitorByIds(ids);
    }

    /**
     * 删除边坡雷达信息
     *
     * @param id 边坡雷达主键
     * @return 结果
     */
    @Override
    public int deleteSlopeRadarMonitorById(Long id) {
        return slopeRadarMonitorMapper.deleteSlopeRadarMonitorById(id);
    }
}
